package com.banmao.mall.controller;

import com.banmao.mall.model.vo.BaseRespVO;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletResponse;

@RestControllerAdvice
public class ExceptionControllerAdvice {

    @ExceptionHandler(NumberFormatException.class)
    public BaseRespVO errorNumberFormat(NumberFormatException e) {
        e.printStackTrace();
        return BaseRespVO.error("参数值不正确", 401);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public BaseRespVO wrongParam(HttpMessageNotReadableException e) {
        e.printStackTrace();
        return BaseRespVO.error("参数值不正确", 401);
    }

    // 未登录异常
    @ExceptionHandler(IllegalAccessException.class)
    public BaseRespVO unLogin(IllegalAccessException e) {

        return BaseRespVO.error("请先登录", 501);
    }

    // 未授权异常
    @ExceptionHandler(UnauthorizedException.class)
    public BaseRespVO unauthorized(UnauthorizedException e) {
        //e.printStackTrace();
        return BaseRespVO.error("该账号没有此页面的授权", 506);
    }

    // 未认证异常
    @ExceptionHandler(UnauthenticatedException.class)
    public BaseRespVO unauthenticated(UnauthenticatedException e) {

        return BaseRespVO.error("请先登录", 501);
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public BaseRespVO maxUploadSize(MaxUploadSizeExceededException e) {

        return BaseRespVO.error("文件过大， 请选择小于5MB的文件", 401);
    }

    // 捕获所有异常
    @ExceptionHandler(Exception.class)
    public BaseRespVO allException(Exception e) {
        e.printStackTrace();
        return BaseRespVO.error("系统内部错误", 502);
    }

}
